//
//  BulletCache.m
//  MyTest1
//
//  Created by 祥 雍 on 12-3-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BulletCache.h"
#import "Bullet.h"
#import "BulletModelInfoMgr.h"

@implementation BulletCache

-(id) init
{
    if ((self = [super init])) 
    {
        // 使用子弹图片
        batch = [CCSpriteBatchNode batchNodeWithFile:@"Battle_480_320.png"]; 
        [self addChild:batch];
        
        //初始化子弹分类字典
        int bulletTypeCount = [[BulletModelInfoMgr sharedManager] getBulletModelCount];
        bulletsDict = [NSMutableDictionary dictionaryWithCapacity:bulletTypeCount];
        [bulletsDict retain];
    }
    return self; 
}

/**
	预生成子弹
	@param bulletId 子弹的Id
	@param number 初始化生成的个数
 */
-(void) preBuildBulletWithId:(int)bulletId
                      number:(int)number
{
    //检查字典里面有没有该类型的，没有就生成
    CCArray* array = [bulletsDict objectForKey:[NSString stringWithFormat:@"%d", bulletId]];
    if(array == nil)
    {
        array = [CCArray array];
        [bulletsDict setObject:array forKey:[NSString stringWithFormat:@"%d", bulletId]];
    }
    else
    {
        CCLOG(@"该类型子弹已经初始化过了%d", bulletId);
        return;
    }
    
    // 初始化时就生成一定数量的子弹,以后可以重复使用 
    for (int i = 0; i < number; i++)
    {
        Bullet* bullet = [Bullet bulletWithId:bulletId];
        bullet.visible = NO;
        bullet.isActive = NO;
        [batch addChild:bullet];
        [array addObject:bullet];
    }
}

/**
	发射子弹
	@param startPosition 开始位置
	@param rotation 角度 
	@param speed 速度
	@param range 子弹的射程
	@param damageType 伤害类型，能量伤害，动能伤害，爆炸伤害
	@param damageValue 伤害值
	@param bulletId 子弹id
	@param isTeam 是不是队伍的子弹
 */
-(void) shootBulletAt:(CGPoint)startPosition
             rotation:(float)rotation
                speed:(float)speed
                range:(float)range
           damageType:(int)damageType
          damageValue:(float)damageValue
             bulletId:(int)bulletId
               isTeam:(BOOL)isTeam
{
    CCArray* bullets = [bulletsDict objectForKey:[NSString stringWithFormat:@"%d", bulletId]];
    if(bullets)
    {
        //遍历一个没有使用到的bullet.此算法以后要优化
        Bullet* bullet;
        
        CCARRAY_FOREACH(bullets, bullet) 
        {
            if(bullet.isActive == NO)//找一个未激活的子弹
            {
                //设置子弹的队伍
                bullet.isTeam = isTeam;
                //发射子弹
                [bullet shootBulletAt:startPosition rotation:rotation speed:speed range:range damageType:damageType damageValue:damageValue];
                return;
            }
        }
    }
}

-(BOOL) isPlayerBulletCollidingWithRect:(CGRect)rect 
{
    return [self isBulletCollidingWithRect:rect isTeam:YES]; 
}

-(BOOL) isEnemyBulletCollidingWithRect:(CGRect)rect 
{
    return [self isBulletCollidingWithRect:rect isTeam:YES]; 
}

-(BOOL) isBulletCollidingWithRect:(CGRect)rect isTeam:(BOOL)isTeam 
{
//    BOOL isColliding = NO;
//    Bullet* bullet;
//    CCARRAY_FOREACH([batch children], bullet) 
//    {
//        
//        if (bullet.visible && usePlayerBullets == bullet.isPlayerBullet) 
//        {
//            if (CGRectIntersectsRect([bullet boundingBox], rect)) 
//            {
//                isColliding = YES;
//                // 通过将子弹的可视状态设为NO把子弹移除
//                bullet.visible = NO;
//                break; 
    
//            }
//        } 
//    }
//    return isColliding;
    return NO;
}


-(void) dealloc
{
    if(bulletsDict)
    {
        [bulletsDict removeAllObjects];
        [bulletsDict release];
        bulletsDict = nil;
    }
    
    [super dealloc];
}

@end
